<template>
  <div class="todo-footer" >
    <label>
      <input type="checkbox" :checked="gouxuan" @change="clickCheckAll"/>
    </label>
    <span>
      <span>已完成{{completed}}</span> / 全部完成{{isAll}}
    </span>
    <button class="btn btn-danger" @click="clear">清除已完成</button>
  </div>
</template>

<script>
export default {
  name: 'Footer',
  props: ['todoinfoList', 'clearTodo'],
  methods: {
    // 清除已完成
    clear() {
      this.clearTodo()
    },
    // 点击底部按钮todo项全选
    // App和Footer进行全局事件总线通信
    clickCheckAll(e) {
      // console.log(e.target.checked)
      this.$bus.$emit('checkall', e.target.checked)
    }
  },
  computed: {
    // 计算已经完成
    completed() {
      return this.todoinfoList.reduce((pre, current) => {
        return pre + (current.done ? 1 : 0)
      }, 0)
    },
    // 计算全部完成
    isAll() {
      return this.todoinfoList.length
    },
    // 底部是否勾选
    gouxuan() {
      return this.completed === this.isAll && this.isAll > 0
    }
  }
}
</script>

<style lang="less" scoped>
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>
